package com.ruoyi.common.utils.bean;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.verify.BaseValidator;
import com.ruoyi.common.verify.annotation.IdCard;
import com.ruoyi.common.verify.annotation.Phone;
import com.ruoyi.common.verify.validator.IdCardValidator;
import com.ruoyi.common.verify.validator.PhoneValidator;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;
import java.lang.reflect.Field;

/**
 * bean字段校验器
 */
public class BeanFiledValidators {
    public static void checkString(Field field, String value) {
        NotNull notNull = field.getAnnotation(NotNull.class);
        NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
        NotBlank notBlank = field.getAnnotation(NotBlank.class);
        Length length = field.getAnnotation(Length.class);
        Pattern pattern = field.getAnnotation(Pattern.class);
        Phone phone = field.getAnnotation(Phone.class);
        IdCard idCard = field.getAnnotation(IdCard.class);
        if (notNull != null) {
            Assert.isTrue(value != null, () -> new ServiceException(notNull.message()));
        }
        if (notBlank != null) {
            Assert.isTrue(StrUtil.isNotBlank(value), () -> new ServiceException(notBlank.message()));
        }
        if (notEmpty != null) {
            Assert.isTrue(StrUtil.isNotEmpty(value), () -> new ServiceException(notEmpty.message()));
        }
        if (length != null && value != null) {
            Assert.isTrue(value.length() >= length.min() && value.length() <= length.max(), () -> new ServiceException(length.message()));
        }
        if (pattern != null) {
            Assert.isTrue(ReUtil.isMatch(pattern.regexp(), value), () -> new ServiceException(pattern.message()));
        }
        if (phone != null) {
            Assert.isTrue(BaseValidator.getValidator(PhoneValidator.class).valid(value, phone), () -> new ServiceException(phone.message()));
        }
        if (idCard != null) {
            Assert.isTrue(BaseValidator.getValidator(IdCardValidator.class).valid(value, idCard), () -> new ServiceException(idCard.message()));
        }
    }

    public static void checkNumber(Field field, Number value) {
        NotNull notNull = field.getAnnotation(NotNull.class);
        Range range = field.getAnnotation(Range.class);
        Max max = field.getAnnotation(Max.class);
        Min min = field.getAnnotation(Min.class);
        if (notNull != null) {
            Assert.isTrue(value != null, () -> new ServiceException(notNull.message()));
        }
        if (range != null && value != null) {
            Assert.isTrue(value.doubleValue() >= range.min() && value.doubleValue() <= range.max(), () -> new ServiceException(range.message()));
        }
        if (max != null && value != null) {
            Assert.isTrue(value.doubleValue() >= max.value(), () -> new ServiceException(max.message()));
        }
        if (min != null && value != null) {
            Assert.isTrue(value.doubleValue() >= min.value(), () -> new ServiceException(min.message()));
        }

    }
}
